using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.VisualBasic.MyServices;

namespace WindowsApplication1
{
    
    public partial class frmHoleBuilder : Form
    {
        [DllImport("user32.dll")]
        static extern bool GetAsyncKeyState(System.Windows.Forms.Keys vKey); 
        PictureBox[] HolePics = new PictureBox[4096];
        string[,] TreeLayout = new string[64, 64];
        string[,] HoleLayout = new string[64, 64];
        string[,] HeightLayout = new string[64, 64];
        int TopAdjust = 5;
        int LeftAdjust = 160;
        int TopCounter = 11;
        int LeftCounter = 11;
        bool DrawBox = false;
        Boolean MouseDown;
        public frmHoleBuilder()
        {
            InitializeComponent();
        }

        private void frmHoleBuilder_Load(object sender, EventArgs e)
        {
            this.KeyDown += new KeyEventHandler(KeyDown_Handle);
            lstOptions.Items.Add("Clear");
            lstOptions.Items.Add("Water");
            lstOptions.Items.Add("Sand");
            lstOptions.Items.Add("Grass");
            lstOptions.Items.Add("Hill");
            lstOptions.Items.Add("Mountain");


            lstOptions.Items.Add("Willow");
            lstOptions.Items.Add("Pine");
            lstOptions.Items.Add("Birch");
            lstOptions.Items.Add("Gardenwood");

            lstOptions.Items.Add("Tee");
            lstOptions.Items.Add("Pin");

        
            CreateBlankHole();
           /* for (X = 0; X == 500; )
            {
                HolePics[X] = new PictureBox(); 
                HolePics[X].Top = Top * X ;
                HolePics[X].Left = Left * X;
                HolePics[X].BackColor = Color.Violet;
                HolePics[X].Visible = true;
                HolePics[X].Image = System.Drawing.Image.FromFile ("c:\\martians.jpg");
                this.Controls.Add(HolePics[X]);
                HolePics[X].Top = Top * X;
                HolePics[X].Left = Left * X;
                HolePics[X].BackColor = Color.Violet;
                HolePics[X].Visible = true;
                HolePics[X].Image = System.Drawing.Image.FromFile("c:\\martians.jpg");
            }*/
        }

        private void CreateBlankHole()
        {
            //int X = 0;
            //int TopCounter = 11;
            //int LeftCounter = 11;
            int PicCounter = 0;
            for (int Y = 0; Y <= 63; Y++)
            {
                
                for (int X = 0; X <= 63; X++)
                {
                    HolePics[PicCounter] = new PictureBox();
                    HolePics[PicCounter].Top = Y * TopCounter + TopAdjust; 
                    HolePics[PicCounter].Height = 10;
                    HolePics[PicCounter].Width = 10;
                    HolePics[PicCounter].BackColor = Color.White;
                    //HolePics[PicCounter].Image = System.Drawing.Image.FromFile("c:\\martians.jpg");
                    //HolePics[X].Top = TopCounter * X + TopAdjust;
                    HolePics[PicCounter].Left = LeftCounter * X + LeftAdjust;
                    HolePics[PicCounter].Visible = true;
                    HolePics[PicCounter].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                    //HolePics[PicCounter].Click += new System.Windows.Forms(Form_MouseClick);
                    HolePics[PicCounter].MouseDown += new System.Windows.Forms.MouseEventHandler(Form_MouseDown);
                    HolePics[PicCounter].MouseMove += new System.Windows.Forms.MouseEventHandler(Form_MouseMove);
                    //HolePics[PicCounter].MouseUp += new System.Windows.Forms.MouseEventHandler(Form_MouseUp);
                    //HolePics[PicCounter].Enter += new System.EventHandler(Form_MouseEnter);
                        //this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter);
                    //HolePics[PicCounter].MouseEnter += new System.Windows.Forms.MouseEventHandler(Form_MouseDown);
                    //HolePics[PicCounter].MouseUp += new System.Windows.Forms.MouseEventHandler(Form_MouseUnClick);
                    this.Controls.Add(HolePics[PicCounter]);
                    PicCounter = PicCounter + 1;
                }
                //MessageBox.Show("Done");
            }
        }

        private void picHole(object sender, EventArgs e)
        {
            PictureBox temp = (PictureBox)sender;
            temp.Image = picPreview.Image;
            try
            {
                HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
            }
            catch
            {
                MessageBox.Show("You must select an item from the options list");
            }
            finally
            {
            }

        }

        private void picPreview_Click(object sender, EventArgs e)
        {

        }

        private void lstOptions_SelectedIndexChanged(object sender, EventArgs e)
        {
            
                    //MessageBox.Show(sender.ToString());
            if (lstOptions.SelectedItem.ToString() != "Clear")
            {
                picPreview.Image = System.Drawing.Image.FromFile(Application.StartupPath + "\\Pictures\\" + lstOptions.SelectedItem.ToString() + ".jpg");
            }
            else
            {
                picPreview.Image = null;
            }
            
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            Color Normal;
            Color Blue;
            Color Red;
            Color Green;
            Color Black;
            Color White;
            Color Sand;
            Color Grass;
            Color Hill;
            Color Mountain;
            Color Water;
            Color Willow;
            Water = Color.FromArgb(0, 0, 0);
            Hill = Color.FromArgb(130, 130, 130);
            Mountain = Color.FromArgb(255, 255, 255);
            Sand = Color.FromArgb(50, 50, 50);
            Grass = Color.FromArgb(110, 110, 110);
            Red = Color.FromArgb(255, 0, 0);
            Green = Color.FromArgb(0, 255, 0);
            Blue = Color.FromArgb(0, 0, 255);
            Black = Color.FromArgb(0, 0, 0);
            White = Color.FromArgb(255, 255, 255);
            Willow = Color.FromArgb(0,0,125);
            Normal = Color.FromArgb(128,128,128);
            Bitmap bmp = new Bitmap(128,128);
            for (int x = 0; x < 64; x++)
            {
                for (int y = 0; y < 64; y++)
                {
                    bmp.SetPixel(y * 2, x * 2, Normal);
                    bmp.SetPixel(y * 2, (x * 2)+1, Normal);
                    bmp.SetPixel((y * 2) +1, x * 2, Normal);
                    bmp.SetPixel((y * 2) +1, (x * 2)+1, Normal);
                    switch(HoleLayout[x,y])
                    {
                        case "Pin":
                            bmp.SetPixel(y*2,x*2,White);
                            break;
                        case "Tee":
                            bmp.SetPixel(y*2, x*2, Black);
                            break;
                        default:
                            bmp.SetPixel(y*2, x*2, Normal);
                            break;
                    } 
                }
            }
            bmp.Save("c:\\dgcourse\\hole1\\holemap.bmp");
            bmp = new Bitmap(128, 128);
            for (int x = 0; x < 64; x++)
            {
                for (int y = 0; y < 64; y++)
                {
                    bmp.SetPixel(y * 2, x * 2, Normal);
                    bmp.SetPixel(y * 2, (x * 2) + 1, Normal);
                    bmp.SetPixel((y * 2) + 1, x * 2, Normal);
                    bmp.SetPixel((y * 2) + 1, (x * 2) + 1, Normal);
                    switch (TreeLayout[x, y])
                    {
                        case "Pine":
                            bmp.SetPixel(y * 2, x * 2, Green);
                            break;
                        case "Birch":
                            bmp.SetPixel(y * 2, x * 2, Red);
                            break;
                        case "Gardenwood":
                            bmp.SetPixel(y * 2, x * 2, Blue);
                            break;
                        case "Willow":
                            bmp.SetPixel(y * 2, x * 2, Willow);
                            break;
                        default:
                            bmp.SetPixel(y * 2, x * 2, Normal);
                            break;
                    } 
                }
            }
            bmp.Save("c:\\dgcourse\\hole1\\treemap.bmp");
            bmp = new Bitmap(128, 128);
            Normal = Color.FromArgb(75, 75, 75);
            for (int x = 0; x < 64; x++)
            {
                for (int y = 0; y < 64; y++)
                {
                    bmp.SetPixel(y * 2, x * 2, Normal);
                    bmp.SetPixel(y * 2, (x * 2) + 1, Normal);
                    bmp.SetPixel((y * 2) + 1, x * 2, Normal);
                    bmp.SetPixel((y * 2) + 1, (x * 2) + 1, Normal);
                    switch (HeightLayout[x, y])
                    {
                        case "Mountain":
                            bmp.SetPixel(y * 2, x * 2, Mountain);
                            bmp.SetPixel(y * 2, (x * 2) + 1, Mountain);
                            bmp.SetPixel((y * 2) + 1, x * 2, Mountain);
                            bmp.SetPixel((y * 2) + 1, (x * 2) + 1, Mountain);
                            break;
                        case "Hill":
                            bmp.SetPixel(y * 2, x * 2, Hill);
                            bmp.SetPixel(y * 2, (x * 2) + 1, Hill);
                            bmp.SetPixel((y * 2) + 1, x * 2, Hill);
                            bmp.SetPixel((y * 2) + 1, (x * 2) + 1, Hill);
                            break;
                        case "Grass":
                            bmp.SetPixel(y * 2, x * 2, Grass);
                            bmp.SetPixel(y * 2, (x * 2) + 1, Grass);
                            bmp.SetPixel((y * 2) + 1, x * 2, Grass);
                            bmp.SetPixel((y * 2) + 1, (x * 2) + 1, Grass);
                            break;
                        case "Sand":
                            bmp.SetPixel(y * 2, x * 2, Sand);
                            bmp.SetPixel(y * 2, (x * 2) + 1, Sand);
                            bmp.SetPixel((y * 2) + 1, x * 2, Sand);
                            bmp.SetPixel((y * 2) + 1, (x * 2) + 1, Sand);
                            break;
                        case "Water":
                            bmp.SetPixel(y * 2, x * 2, Water);
                            bmp.SetPixel(y * 2, (x * 2) + 1, Water);
                            bmp.SetPixel((y * 2) + 1, x * 2, Water);
                            bmp.SetPixel((y * 2) + 1, (x * 2) + 1, Water);
                            break;
                        default:
                            bmp.SetPixel(y * 2, x * 2, Normal);
                            bmp.SetPixel(y * 2, (x * 2) + 1, Normal);
                            bmp.SetPixel((y * 2) + 1, x * 2, Normal);
                            bmp.SetPixel((y * 2) + 1, (x * 2) + 1, Normal);
                            break;
                    }
                }
            }
            SmoothTerrain(Convert.ToInt32(txtSmoothing.Text), bmp);
            MessageBox.Show("done");
        }



        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            
            if(Control.ModifierKeys == Keys.Control)
                DrawBox = true;
            
            
            PictureBox temp = (PictureBox)sender;
            temp.Image = picPreview.Image;

            //MessageBox.Show(Convert.ToString(((temp.Top - TopAdjust) / (TopCounter)) * 64 + (((temp.Left - LeftAdjust) / (LeftCounter)))));
            if (DrawBox)
            {
                HolePics[(((temp.Top - TopAdjust) / (TopCounter)) * 64) + (((temp.Left - LeftAdjust) / (LeftCounter)))].Image = picPreview.Image;
                HolePics[(((temp.Top - TopAdjust) / (TopCounter)) * 64) + (((temp.Left - LeftAdjust) / (LeftCounter))) + 1].Image = picPreview.Image;
                HolePics[(((temp.Top - TopAdjust) / (TopCounter)) * 64) + (((temp.Left - LeftAdjust) / (LeftCounter))) + 64].Image = picPreview.Image;
                HolePics[(((temp.Top - TopAdjust) / (TopCounter)) * 64) + (((temp.Left - LeftAdjust) / (LeftCounter))) + 65].Image = picPreview.Image;
            }
            //MessageBox.Show("X, " + Convert.ToString((temp.Top - TopAdjust) / TopCounter) + "Y, " + Convert.ToString((temp.Left - LeftAdjust) / (LeftCounter)));
            try
            {
                if (!DrawBox)
                {
                    switch (lstOptions.SelectedItem.ToString())
                    {
                        case "Clear":
                            HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                            break;
                        case "Pin":
                            HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Tee":
                            HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Water":
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Grass":
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Hill":
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Mountain":
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Willow":
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Pine":
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Gardenwood":
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Birch":
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Sand":
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            break;
                    }
                }
                else
                {
                    switch (lstOptions.SelectedItem.ToString())
                    {
                        case "Clear":
                            HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                            HoleLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                            HoleLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = "";
                            HoleLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = "";
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                            HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                            HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = "";
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = "";
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                            TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                            TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = "";
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = "";
                            break;
                        case "Pin":
                            HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HoleLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HoleLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            HoleLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Tee":
                            HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HoleLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HoleLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            HoleLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Water":
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Grass":
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Hill":
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Mountain":
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Willow":
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Pine":
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Gardenwood":
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Birch":
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            TreeLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            break;
                        case "Sand":
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                            HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            HeightLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                            break;
                    }
                }
            }
            catch
            {
                MessageBox.Show("You must select an item from the options list");
            }
            finally
            {
            }

        }
        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            MouseDown = false;

        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if ((Control.ModifierKeys & Keys.Shift)!=0)
            {
                if ((Control.ModifierKeys & Keys.Control)!=0)
                    DrawBox = true;
                PictureBox temp = (PictureBox)sender;
                temp.Image = picPreview.Image;

                //MessageBox.Show(Convert.ToString(((temp.Top - TopAdjust) / (TopCounter)) * 64 + (((temp.Left - LeftAdjust) / (LeftCounter)))));
                try
                {
                if (DrawBox)
                {
                    HolePics[(((temp.Top - TopAdjust) / (TopCounter)) * 64) + (((temp.Left - LeftAdjust) / (LeftCounter)))].Image = picPreview.Image;
                    HolePics[(((temp.Top - TopAdjust) / (TopCounter)) * 64) + (((temp.Left - LeftAdjust) / (LeftCounter))) + 1].Image = picPreview.Image;
                    HolePics[(((temp.Top - TopAdjust) / (TopCounter)) * 64) + (((temp.Left - LeftAdjust) / (LeftCounter))) + 64].Image = picPreview.Image;
                    HolePics[(((temp.Top - TopAdjust) / (TopCounter)) * 64) + (((temp.Left - LeftAdjust) / (LeftCounter))) + 65].Image = picPreview.Image;
                }
                //MessageBox.Show("X, " + Convert.ToString((temp.Top - TopAdjust) / TopCounter) + "Y, " + Convert.ToString((temp.Left - LeftAdjust) / (LeftCounter)));
                
                    if (!DrawBox)
                    {
                        switch (lstOptions.SelectedItem.ToString())
                        {
                            case "Clear":
                                HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                                break;
                            case "Pin":
                                HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Tee":
                                HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Water":
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Grass":
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Hill":
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Mountain":
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Willow":
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Pine":
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Gardenwood":
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Birch":
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Sand":
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                break;
                        }
                    }
                    else
                    {
                        switch (lstOptions.SelectedItem.ToString())
                        {
                            case "Clear":
                                HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                                HoleLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                                HoleLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = "";
                                HoleLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = "";
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                                HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                                HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = "";
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = "";
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                                TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = "";
                                TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = "";
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = "";
                                break;
                            case "Pin":
                                HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HoleLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HoleLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                HoleLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Tee":
                                HoleLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HoleLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HoleLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                HoleLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Water":
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Grass":
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Hill":
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Mountain":
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Willow":
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Pine":
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Gardenwood":
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Birch":
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                TreeLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                TreeLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                break;
                            case "Sand":
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, (temp.Left - LeftAdjust) / (LeftCounter)] = lstOptions.SelectedItem.ToString();
                                HeightLayout[((temp.Top - TopAdjust) / (TopCounter)) + 1, ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                HeightLayout[(temp.Top - TopAdjust) / (TopCounter), ((temp.Left - LeftAdjust) / (LeftCounter)) + 1] = lstOptions.SelectedItem.ToString();
                                break;
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("You must select an item from the options list");
                }
                finally
                {
                }
            }
            DrawBox = false;
        }
        private void SmoothTerrain(int Passes, Bitmap HeightMap)
        {
            float[,] newHeightData;
            int MapWidth = 128;
            int MapHeight = 128;
            float[,] HeightData = new float[128,128];
            for (int x = 0; x < 128; x++)
            {
                for (int y = 0; y < 128; y++)
                {
                    HeightData[x, y] = HeightMap.GetPixel(x, y).R;
                }
            }
            while (Passes > 0)
            {
                Passes--;

                // Note: MapWidth and MapHeight should be equal and power-of-two values
                newHeightData = new float[MapWidth, MapHeight];

                for (int x = 0; x < MapWidth; x++)
                {
                    for (int y = 0; y < MapHeight; y++)
                    {
                        int adjacentSections = 0;
                        float sectionsTotal = 0.0f;

                        if ((x - 1) > 0) // Check to left
                        {
                            sectionsTotal += HeightData[x - 1, y];
                            adjacentSections++;

                            if ((y - 1) > 0) // Check up and to the left
                            {
                                sectionsTotal += HeightData[x - 1, y - 1];
                                adjacentSections++;
                            }

                            if ((y + 1) < MapHeight) // Check down and to the left
                            {
                                sectionsTotal += HeightData[x - 1, y + 1];
                                adjacentSections++;
                            }
                        }

                        if ((x + 1) < MapWidth) // Check to right
                        {
                            sectionsTotal += HeightData[x + 1, y];
                            adjacentSections++;

                            if ((y - 1) > 0) // Check up and to the right
                            {
                                sectionsTotal += HeightData[x + 1, y - 1];
                                adjacentSections++;
                            }

                            if ((y + 1) < MapHeight) // Check down and to the right
                            {
                                sectionsTotal += HeightData[x + 1, y + 1];
                                adjacentSections++;
                            }
                        }

                        if ((y - 1) > 0) // Check above
                        {
                            sectionsTotal += HeightData[x, y - 1];
                            adjacentSections++;
                        }

                        if ((y + 1) < MapHeight) // Check below
                        {
                            sectionsTotal += HeightData[x, y + 1];
                            adjacentSections++;
                        }

                        newHeightData[x, y] = (HeightData[x, y] + (sectionsTotal / adjacentSections)) * 0.5f;
                    }
                }

                // Overwrite the HeightData info with our new smoothed info
                for (int x = 0; x < MapWidth; x++)
                {
                    for (int y = 0; y < MapHeight; y++)
                    {
                        HeightData[x, y] = newHeightData[x, y];
                    }
                }
            }
            for (int x = 0; x < MapWidth; x++)
            {
                for (int y = 0; y < MapHeight; y++)
                {
                    Color NewColor;
                    NewColor = Color.FromArgb((int)HeightData[x, y], (int)HeightData[x, y], (int)HeightData[x, y]);
                    HeightMap.SetPixel(x,y,NewColor);
                }
            }
            HeightMap.Save("c:\\dgcourse\\hole1\\heightmap.bmp");
        }
        private void KeyDown_Handle(object Sender, KeyEventArgs e)
        {
            string c = e.KeyCode.ToString();
            if (c == "z")
            {
                if (!DrawBox)
                    DrawBox = true;
                else
                    DrawBox = false;
            }
            MessageBox.Show(DrawBox.ToString());
        }

    }
        

    }

